package com.lovo.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.bean.Role;
import com.lovo.bean.RoleDistribution;
import com.lovo.bean.Throne;
import com.lovo.dao.RoleMapper;
import com.lovo.dao.ThroneMapper;
import com.lovo.service.RoleService;
import com.lovo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    @Override
    public PageInfo<Role> getRoles(int pageNo, int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        PageHelper.startPage(pageNo,parseInt);
        List<Role> list = mapper.selectAll();
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public boolean addRole(Role r) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        int i = mapper.insertSelective(r);
        SqlSessionUtil.commit();
        return i>0;
    }

    @Override
    public PageInfo<Role> getRolesByQuery(int pageNo, int parseInt, String roleName) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        PageHelper.startPage(pageNo,parseInt);
        List<Role> list = mapper.selectAllByQuery(roleName);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public boolean deleteRole(int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        int i = mapper.deleteByPrimaryKey(parseInt);
        SqlSessionUtil.commit();
        return i>0;
    }

    @Override
    public PageInfo<Throne> getRoleThrone(int pageNo, int parseInt, int parseInt1) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        ThroneMapper mapper = sqlSession.getMapper(ThroneMapper.class);
        PageHelper.startPage(pageNo,parseInt);
        List<Throne> list = mapper.checkRole(parseInt1);
        PageInfo<Throne> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public PageInfo<RoleDistribution> getAllRoles() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        List<Role> list = mapper.getAllRoles();
        List<RoleDistribution> list1 = new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            RoleDistribution rd = new RoleDistribution();
            rd.setKey(list.get(i).getRoleId());
            rd.setLabel(list.get(i).getRoleName());
            list1.add(rd);
        }
        PageInfo<RoleDistribution> pageInfo = new PageInfo<>(list1);
        SqlSessionUtil.commit();
        return pageInfo;
    }

    @Override
    public PageInfo<Integer> getUserRole(int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        List<Integer> list = mapper.getUserRole(parseInt);
        PageInfo<Integer> pageInfo = new PageInfo<>(list);
        SqlSessionUtil.commit();
        return pageInfo;
    }
}
